package com.ch.home.service.serviceImpl.userServiceImpl;

import com.ch.home.bean.UserComment;
import com.ch.home.mapper.userMessageMapper.UserCommentMapper;
import com.ch.home.service.serviceInterface.user.UserCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by CHEN on 2019/8/17.
 */
@Service
public class UserCommentServiceImpl implements UserCommentService{


    @Autowired
    UserCommentMapper userCommentMapper;

    /**
     * 根据评论id查评论
     * @param commentId
     * @return
     */
    @Override
    public HashMap selectByCommentId(int commentId) {


        HashMap result = new HashMap( );
        UserComment userComment = userCommentMapper.selectByCondition(commentId);
        if (userComment == null) {
            result.put("message", "fail");
            result.put("value", null);
        } else {
            result.put("message", "success");
            result.put("value", userComment);
        }
        return result;

    }

    /**
     * 根据id查出所有符合
     * @return
     */
    @Override
    public HashMap selectByUserId(UserComment userComment) {

        HashMap<Object,Object> result = new HashMap();
        LinkedList<UserComment> userComments = userCommentMapper.selectAllByCondition(userComment);

        if (userComments.size()==0){
            result.put("message","Empty!");
        }else {
            result.put("message","success");
        }
        result.put("value",userComments);
        return result;
    }


    /**
     * 新增评论
     *
     * @return
     */
    @Override
    public HashMap insertUserComment(UserComment userComment) {

        HashMap<Object, Object> result = new HashMap<>( );

        System.out.println(userComment);

        Date date = new Date();
        userComment.setTime(date);
        if (userCommentMapper.insertUserComment(userComment) == 1) {
            result.put("message", "seccess");
        } else {
            result.put("message", "fail");
        }
        return result;

    }


    /**
     * 修改点赞数、时间、状态
     * @param userComment
     * @return
     */
    @Override
    public HashMap updateUser(UserComment userComment){

        HashMap<Object, Object> result = new HashMap<>( );

        Date date = new Date();
        userComment.setTime(date);

        System.out.println(userComment );
        Integer re=userCommentMapper.updateUserCommentByCondition(userComment);
        if (re==1){
            if (userComment.getStatus()!=null&&userComment.getStatus()==0)
                result.put("message","success delete!");
            if (userComment.getStatus()!=null&&userComment.getStatus()==1)
                result.put("message","success find back!");
            if (userComment.getLikeNum()!=null&&userComment.getLikeNum()==1)
                result.put("message","success like!");
            if (userComment.getLikeNum()!=null&&userComment.getLikeNum()==-1)
                result.put("message","success unlike!");
        }else {
            result.put("message","fail");
        }

        return result;
    }


//    /**
//     * 将map中的值封装进UserComment中
//     *
//     * @param map
//     * @return
//     */
//    public UserComment getUserCommentFromMap(HashMap map) {
//        Integer commentId = (Integer) map.get("commentId");//评论id
//        Integer userId = (Integer) map.get("userId");//发评用户id
//        Integer shareContentId = (Integer) map.get("shareContentId");//资源分享者id
//        Integer parentCommentId = (Integer) map.get("parentCommentId");//上机评论id
//        Date time = (Date) map.get("time");//时间
//        String content = (String) map.get("content");//评论内容
//        Integer likeNum = (Integer) map.get("likeNum");//点赞数
//        Integer status = (Integer) map.get("status");//评论状态：0=已删除，1=正常
//        UserComment userComment = new UserComment(commentId, userId, shareContentId, parentCommentId, time, content, likeNum, status);
//        return userComment;
//
//    }


}
